import app_config as config
from .file_gen import *


# 获取 zookeeper id列表: xiaoma01,xiaoma02,xiaoma03
def getZookeeperIds() -> str:
    zk_nodes = config.zookeeper_config.get('nodes')
    zks = []
    for zk in zk_nodes:
        zks.append(zk.get('host'))

    return ','.join(zks)


# 获取 zookeeper 节点列表: xiaoma01:2181,xiaoma02:2181,xiaoma03:2181
def getZookeeperNodes() -> str:
    zk_nodes = config.zookeeper_config.get('nodes')
    zks = []
    for zk in zk_nodes:
        zks.append('%s:%d' % (zk.get('host'), zk.get('port')))

    return ','.join(zks)


class YarnSiteCreator(FileCreator):
    tmp = '''
    <property>
        <name>yarn.resourcemanager.hostname.rm%d</name>
        <value>%s</value>
    </property>'''

    def getFileName(self):
        return 'yarn-site.xml'

    def initData(self):
        # 设置注释标记模式，会自动删除带 @ 的注释
        self.examplePattern = eXmlPattern

        rms = config.hadoop_config.get('resource_managers')

        # rm-ids: rm1,rm2
        rm_ids = []
        for i in range(len(rms)):
            rm_ids.append('rm' + str(i + 1))

        self.data['yarn.resourcemanager.ha.rm-ids'] = ','.join(rm_ids)

        # rm-list
        rm_list = []
        for i in range(len(rms)):
            rm_list.append(self.tmp % (i + 1, rms[i].get('host')))

        self.data['rm-list'] = ''.join(rm_list)

        # zks: xiaoma01:2181,xiaoma02:2181,xiaoma03:2181
        self.data['yarn.resourcemanager.zk-address'] = getZookeeperNodes()


class CoreSiteCreator(FileCreator):
    def getFileName(self):
        return 'core-site.xml'

    def initData(self):
        self.examplePattern = eXmlPattern

        self.data['ha.zookeeper.quorum'] = getZookeeperNodes()
        self.data['hadoop_home'] = config.hadoop_config.get('home')


class HdfsSiteCreator(FileCreator):
    rpc_temp = '''
    <property>
        <name>dfs.namenode.rpc-address.cluster.nn%d</name>
        <value>%s:%d</value>
    </property>'''

    http_temp = '''
    <property>
        <name>dfs.namenode.http-address.cluster.nn%d</name>
        <value>%s:%d</value>
    </property>'''

    def getFileName(self):
        return 'hdfs-site.xml'

    def initData(self):
        self.examplePattern = eXmlPattern

        self.data['user_home'] = config.user_config.get('home')
        self.data['hadoop_home'] = config.hadoop_config.get('home')
        self.data['dfs.replication'] = str(config.hadoop_config.get('dfs.replication'))

        nodes = config.hadoop_config.get('name_nodes')

        # name nodes: nn1,nn2,nn3
        n_nodes = []
        for i in range(len(nodes)):
            n_nodes.append('nn%d' % (i + 1))

        self.data['dfs.ha.namenodes.cluster'] = ','.join(n_nodes)

        # rpc list
        rpcs = []
        for i in range(len(nodes)):
            item = self.rpc_temp % (i + 1, nodes[i].get('host'), nodes[i].get('rpc'))
            rpcs.append(item)

        self.data['rpc-list'] = ''.join(rpcs)

        # http list
        https = []
        for i in range(len(nodes)):
            item = self.http_temp % (i + 1, nodes[i].get('host'), nodes[i].get('http'))
            https.append(item)

        self.data['http-list'] = ''.join(https)

        # journal_nodes
        j_nodes = config.hadoop_config.get('journal_nodes')
        jns = []
        for node in j_nodes:
            item = '%s:%d' % (node.get('host'), node.get('port'))
            jns.append(item)

        self.data['journal_nodes'] = ';'.join(jns)  # 这里是分号


class MapredSiteCreator(FileCreator):
    def getFileName(self):
        return 'mapred-site.xml'

    def initData(self):
        self.examplePattern = eXmlPattern

        self.data['hadoop_home'] = config.hadoop_config.get('home')


class WorkersCreator(FileCreator):
    def getFileName(self):
        return 'workers'

    def initData(self):
        workers = []
        for node in config.hadoop_config.get('data_nodes'):
            workers.append(node.get('host'))

        self.data['workers'] = '\n'.join(workers)


class HadoopEnvCreator(FileCreator):
    def getFileName(self):
        return 'hadoop-env.sh'

    def initData(self):
        self.examplePattern = eShPattern
        self.data['java_home'] = config.java_config.get('home')


class HadoopConfigManager(FileManager):
    def init(self):
        # 创建文件生成器
        self.creators = [
            YarnSiteCreator(self.importDir, self.exportDir),
            CoreSiteCreator(self.importDir, self.exportDir),
            HdfsSiteCreator(self.importDir, self.exportDir),
            MapredSiteCreator(self.importDir, self.exportDir),
            WorkersCreator(self.importDir, self.exportDir),
            HadoopEnvCreator(self.importDir, self.exportDir)
        ]
